<?php

class Application_Model_LineasTelefonicas extends Sgit_Model
{  
    protected $_name = 'lineas';
    protected $_primary = 'id';


    public function getLinea($id)
    {
        return $this->getLineas(array(), $id);
    }


    public function getLineas($values = array(), $id = null)
    {

        $select = $this->getAdapter()->select();
        $select->from(array($this->_name));
        $select->joinLeft('proyectos_circuitos', 'proyectos_circuitos.id = lineas.id_proyecto', array('proyecto'=>'nombre'));
        $select->joinLeft('salas', 'salas.id = lineas.id_sala', array('sala'=>'nombre'));
       
       
        if (!is_null($id))
        {
            $select->where("lineas.id= ?", $id);

        } else
        {
            if (is_numeric($values['id_zona']))
                $select->where('lineas.id_zona= ?', $values['id_zona'],INTEGER);
            if (is_numeric($values['id_proyecto']))
                $select->where('lineas.id_proyecto= ?', $values['id_proyecto'],INTEGER);
            if (is_numeric($values['id_sala']))
                $select->where('lineas.id_sala= ?', $values['id_sala'],INTEGER);
            if ($values['numero'])
                $select->where("lineas.numero LIKE ?", "%{$values['numero']}%");
            if ($values['administrativo'])
                $select->where('lineas.administrativo LIKE ?', "%{$values['administrativo']}%");
            if (is_numeric($values['grupo']))
                $select->where('lineas.grupo LIKE ?', "%{$values['grupo']}%");
           if (is_numeric($values['par']))
                $select->where('lineas.par LIKE ?', "%{$values['par']}%");

        }


        $rows = $this->getAdapter()->fetchAll($select);

        if (is_null($id))
            return $rows;
        else
            return $rows[0];


    }
    
    function testEspejo($id_zona,$espejo,$par)
    {
        $select = $this->getAdapter()->select();
        $select->from(array($this->_name),array('c'=>new Zend_Db_Expr('count(*)')));
        $select->where('lineas.id_zona= ?', $id_zona,INTEGER);
        $select->where('lineas.espejo= ?', $espejo);
        $select->where('lineas.espejo_par= ?', $par);
        
        return $this->getAdapter()->fetchOne($select);
    }
    

    function testGrupo($id_zona,$grupo,$par)
    {
        $select = $this->getAdapter()->select();
        $select->from(array($this->_name),array('c'=>new Zend_Db_Expr('count(*)')));
        $select->where('lineas.id_zona= ?', $id_zona,INTEGER);
        $select->where('lineas.grupo= ?', $grupo);
        $select->where('lineas.par= ?', $par);
        
        return $this->getAdapter()->fetchOne($select);
    }       
    
} 




